package park.gz.com.parkingfee.activity;

import android.app.Application;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;


import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.DbManager;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import park.gz.com.parkingfee.bean.UserBean;
import park.gz.com.parkingfee.until.ShareUtil;

/**
 * Created by Endeavor on 2016/8/26.
 */
public class MyAppcation extends Application  {

    //public static String baseUrl = "";//测试接口环境
    public static String userName = "username" ;
    public static DbManager.DaoConfig daoConfig;
    public static List<UserBean> mAccounts = new ArrayList<UserBean>();
    public static String userInfo="saveuserinfo";
    @Override
    public void onCreate() {
        super.onCreate();

        x.Ext.init(this);
        daoConfig=new DbManager.DaoConfig().setDbName("park.db")
                .setDbUpgradeListener(new DbManager.DbUpgradeListener() {
                    @Override
                    public void onUpgrade(DbManager db, int oldVersion, int newVersion) {

                    }
                });
        ShareUtil.setstringvalue(this,"baseurl","http://pass.mylife114.com/");
        authorize();
    }


    /**
     *获取code
     */
    public void authorize() {

        String url = ShareUtil.getstringvalue(MyAppcation.this,"baseurl") + "/oauth/authorize";
        RequestParams params = new RequestParams(url);
        params.addParameter("client_id", "27d99e12ed99d17fca7602bba157f0cea94d4e49d740f52303944911e723b69e");
        params.addParameter("redirect_uri", "urn:ietf:wg:oauth:2.0:oob");
        params.addParameter("response_type", "code");
        Log.i("duzhijuan", params + "");
        x.http().get(params, new Callback.CommonCallback<String>() {

            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    if ("1".equals(jsonObject.getString("ref"))) {
                        access_token(jsonObject.getString("code"));
                    } else {
                        Toast.makeText(MyAppcation.this, jsonObject.getString("msg"), Toast.LENGTH_LONG).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.e("my", "onError" + ex.toString());
            }

            @Override
            public void onCancelled(CancelledException cex) {
                Log.e("my", "onCancelled" + cex.toString());
            }

            @Override
            public void onFinished() {
                Log.e("my", "onFinished");
            }

        });
    }

    /**
     * 获取token
     * @param code
     */
    private void access_token(String code) {
        String url = ShareUtil.getstringvalue(MyAppcation.this,"baseurl") + "/oauth/token";
        RequestParams params = new RequestParams(url);
        params.addParameter("client_id", "27d99e12ed99d17fca7602bba157f0cea94d4e49d740f52303944911e723b69e");
        params.addParameter("redirect_uri", "urn:ietf:wg:oauth:2.0:oob");
        params.addParameter("client_secret", "29f2d01ffc27c6f759626ae3772c1e8776c1744225378811c8f25c0a67f72367");
        params.addParameter("code", code);
        params.addParameter("grant_type", "authorization_code");
        Log.i("duzhijuan", params + "");
        x.http().post(params, new Callback.CommonCallback<String>() {

            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    ShareUtil.setstringvalue(MyAppcation.this,"access_token",jsonObject.getString("access_token"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.e("my", "onError" + ex.toString());
            }

            @Override
            public void onCancelled(CancelledException cex) {
                Log.e("my", "onCancelled" + cex.toString());
            }

            @Override
            public void onFinished() {
                Log.e("my", "onFinished");
            }

        });
    }


}
